//
//  DDMyQuestionViewController.m
//  dingdang
//
//  Created by Chen Jing on 15-7-20.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDMyQuestionViewController.h"
#import "DDNetWorkAPIClient.h"
#import "JSONKit.h"
#import "DDModelMyQuetionsList.h"
#import "DDMyQuestionCollectionViewCell.h"
#import "MJRefresh.h"
#import "DateUtil.h"
#import "DDTypeDefine.h"
#import "ModelQuestionCourse.h"
#import "DDMyQuestionDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "DDQuestionBaseInfoViewController.h"
#import "DDQuestionChooseCourseViewController.h"
#import "DDTopic.h"
#import "DDZdtjVC.h"

@interface DDMyQuestionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIPickerViewDataSource,UIPickerViewDelegate,DDMyQuestionCollectionViewCellDelegate,UIAlertViewDelegate,UIActionSheetDelegate,DDQuestionChooseCourseViewControllerDelegate>{
    CGRect hold_chooseViewFrame;
    DDModelMyQuetionsList * hold_model;
    NSRange timeRange;
    NSInteger currentPage;
    NSMutableDictionary * hold_param;
    DDNetWorkAPIClient * client;

    NSMutableArray * array_question_help;
    NSMutableArray * array_question_reward;
    NSMutableArray * array_question_encryption;
    NSMutableArray * array_questionType;
    NSMutableArray * array_questionPublishType;

    NSMutableArray * array_courseType;

    NSString * param_currentChoosedPublishType;
    ModelQuestionCourseElement * param_choosedCourse;
    NSString * param_choosedQuestionType;

    NSString * param_temp_currentChoosedPublishType;
    ModelQuestionCourseElement * param_temp_choosedCourse;
    NSString * param_temp_choosedQuestionType;

    NSMutableParagraphStyle *paragraphStyle;


    ModelQuestionCourseElement * holdCourseElement;
    DDModelMyQuetionsListElement * holdUpdatePwd_model;
    DDModelMyQuetionsListElement * holdRepublish_model;
    DDModelMyQuetionsListElement * holdChangeCourse_model;
}

@property (weak, nonatomic) IBOutlet UIView *contentOfChoosePickerView;
@property (weak, nonatomic) IBOutlet UIButton *btn_all;
@property (weak, nonatomic) IBOutlet UIButton *btn_course;
@property (weak, nonatomic) IBOutlet UIButton *btn_subject;
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;
@property (weak, nonatomic) IBOutlet UIPickerView *picker_choose;
@end

@implementation DDMyQuestionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
    [self createData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)createUI{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    
    //自定义TAB BAR UI
    [self addNavigatorTitle:@"我的发问" parent:self.view];
    self.barViewControl.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_Background"]];

    [self AddLeftBtnAction:@"" normal:@"top_返回" selected:@"top_返回" action:^{
        [self.navigationController popViewControllerAnimated:YES];
    }];

    [self.myCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([DDMyQuestionCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([DDMyQuestionCollectionViewCell class])];

    [self.myCollectionView addHeaderWithTarget:self action:@selector(getData_refresh)];
    [self.myCollectionView addFooterWithTarget:self action:@selector(getData_loadMore)];

    self.myCollectionView.headerPullToRefreshText = @"下拉可以刷新了";
    self.myCollectionView.headerReleaseToRefreshText = @"松开马上刷新了";
    self.myCollectionView.headerRefreshingText = @"别再拉了,裤子要被拉下来了...";

    self.myCollectionView.footerPullToRefreshText = @"上提可以刷新了";
    self.myCollectionView.footerReleaseToRefreshText = @"松开马上刷新了";
    self.myCollectionView.footerRefreshingText = @"别再拉了,裤子要被拉下来了...";

}

-(void)viewWillAppear:(BOOL)animated{
    self.btn_course.center = CGPointMake(self.view.frame.size.width / 2.0, 20);
//    [self.myCollectionView headerBeginRefreshing];
    [self getData_refresh];
}


-(void)createData{
    hold_param = [NSMutableDictionary dictionary];

    array_question_encryption = [NSMutableArray array];
    array_question_help = [NSMutableArray array];
    array_question_reward = [NSMutableArray array];
    array_questionPublishType = [NSMutableArray array];
    array_questionType = [NSMutableArray array];
    array_courseType = [NSMutableArray array];

    holdCourseElement = [[ModelQuestionCourseElement alloc]init];
    holdCourseElement.info_title = @"科目类型";
    [array_courseType addObject:holdCourseElement];

    [array_questionPublishType addObjectsFromArray:@[@"全部",@"加密",@"悬赏",@"大家帮"]];

    [array_questionType addObject:@"全部"];
    [array_questionType addObjectsFromArray:QuestionTypeArray];

    param_choosedCourse = holdCourseElement;
    param_choosedQuestionType = [array_questionType firstObject];
    param_currentChoosedPublishType = [array_questionPublishType firstObject];

    client = [[DDNetWorkAPIClient alloc]init];

    [client mainCoursetList:@{@"type" : [NSNumber numberWithInteger:DDType_question_questionPublishType_help]} completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
        if (isSuccess) {
            ModelQuestionCourse * tempModel = object;
            [array_question_help addObjectsFromArray:tempModel.info_data];
        }
    }];

    [client mainCoursetList:@{@"type" : [NSNumber numberWithInteger:DDType_question_questionPublishType_Encryption]} completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
        if (isSuccess) {
            ModelQuestionCourse * tempModel = object;
            [array_question_encryption addObjectsFromArray:tempModel.info_data];
        }
    }];

//    [client mainCoursetList:@{@"type" : [NSNumber numberWithInteger:DDType_question_questionPublishType_Reward]} completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
//        if (isSuccess) {
//            ModelQuestionCourse * tempModel = object;
//            [array_question_reward addObjectsFromArray:tempModel.info_data];
//        }
//    }];
    
    [client mainDefaultCourseList:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
        if (isSuccess) {
            ModelQuestionCourse * tempModel = object;
            [array_question_reward addObjectsFromArray:tempModel.info_data];
        }
    }];



    paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 1;

}

-(void)assembleDic{
    [hold_param removeAllObjects];

    [hold_param setObject:[NSNumber numberWithInteger:currentPage] forKey:@"pageIndex"];


    if (![param_currentChoosedPublishType isEqualToString:@"全部"]) {
        if ([param_currentChoosedPublishType isEqualToString:@"大家帮"]) {
            [hold_param setObject:[NSNumber numberWithInteger:0] forKey:@"type"];
        }else if ([param_currentChoosedPublishType isEqualToString:@"加密"]){
            [hold_param setObject:[NSNumber numberWithInteger:1] forKey:@"type"];
        }else{
            [hold_param setObject:[NSNumber numberWithInteger:2] forKey:@"type"];
        }
    }

    if (param_choosedCourse != holdCourseElement) {
        [hold_param setObject:[NSNumber numberWithInteger:param_choosedCourse.info_id.integerValue] forKey:@"course"];
    }


    if (![param_choosedQuestionType isEqualToString:@"全部"]) {
        NSString * questionType_id = [QuestionTypeDic objectForKey:param_currentChoosedPublishType];
        [hold_param setObject:questionType_id forKey:@"questionType"];
    }


}

-(void)getData_refresh{
    timeRange.location = 0;
    timeRange.length = 10;

    currentPage = 0;

    [self assembleDic];

    [client mainMyTopicList:hold_param completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
        NSDictionary * dataDic = (NSDictionary *)object;
        DDModelMyQuetionsList * list = [[DDModelMyQuetionsList alloc]initWithDictionary:dataDic error:nil];
        if (list) {
            hold_model = list;
            if (hold_model.info_list.count < hold_model.info_total.integerValue) {
                currentPage++;
            }
            [self.myCollectionView reloadData];
        }
        [self.myCollectionView headerEndRefreshing];
    }];
}

-(void)getData{
    timeRange.location = 0;
    timeRange.length = 10;
    
    currentPage = 0;
    
    [self assembleDic];
    
    [client mainMyTopicList:hold_param completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
        NSDictionary * dataDic = (NSDictionary *)object;
        DDModelMyQuetionsList * list = [[DDModelMyQuetionsList alloc]initWithDictionary:dataDic error:nil];
        if (list) {
            hold_model = list;
            if (hold_model.info_list.count < hold_model.info_total.integerValue) {
                currentPage++;
            }
            [self.myCollectionView reloadData];
        }
    }];
}

-(void)getData_loadMore{

    if (currentPage < 0) {
        
        [self.myCollectionView footerEndRefreshing];
        return;
    }
    
    [self assembleDic];
    [client mainMyTopicList:hold_param completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
        NSDictionary * dataDic = (NSDictionary *)object;
        DDModelMyQuetionsList * list = [[DDModelMyQuetionsList alloc]initWithDictionary:dataDic error:nil];
        if (hold_model.info_list.count < list.info_total.integerValue) {
            [hold_model.info_list addObjectsFromArray:list.info_list];
            currentPage++;
            [self.myCollectionView reloadData];

        }else{
            currentPage = -1;
        }

        [self.myCollectionView footerEndRefreshing];
    }];
}



- (IBAction)btn_optionAll:(id)sender {
    [self showChooseView];
}

- (IBAction)btn_optionCourse:(id)sender {
    [self showChooseView];
}
- (IBAction)btn_optionSubject:(id)sender {
    [self showChooseView];
}




- (IBAction)btn_cancelChoose:(id)sender {
    [self hiddenChooseView];
    param_temp_choosedQuestionType = nil;
    param_temp_choosedCourse = nil;
    param_temp_currentChoosedPublishType = nil;
}
- (IBAction)btn_confirm:(id)sender {
    if (param_temp_currentChoosedPublishType) {
        param_currentChoosedPublishType = param_temp_currentChoosedPublishType;
    }
    if (param_temp_choosedCourse) {
        param_choosedCourse = param_temp_choosedCourse;
    }
    if (param_temp_choosedQuestionType) {
        param_choosedQuestionType = param_temp_choosedQuestionType;
    }
    [self hiddenChooseView];

    
    [_btn_all setTitle:param_currentChoosedPublishType forState:UIControlStateNormal];
    [_btn_subject setTitle:param_choosedCourse.info_title forState:UIControlStateNormal];
    
//    [self.myCollectionView headerBeginRefreshing];
    [self getData];

}

-(void)showChooseView{
    if (hold_chooseViewFrame.origin.y == 0) {
        hold_chooseViewFrame = self.contentOfChoosePickerView.frame;
    }
    if (self.contentOfChoosePickerView.frame.origin.y == (hold_chooseViewFrame.origin.y - hold_chooseViewFrame.size.height)) {
        return;
    }
    [UIView animateWithDuration:.35 animations:^{
        self.contentOfChoosePickerView.frame = CGRectMake(0, hold_chooseViewFrame.origin.y - hold_chooseViewFrame.size.height, hold_chooseViewFrame.size.width, hold_chooseViewFrame.size.height);
    }];
}

-(void)hiddenChooseView{
    if (hold_chooseViewFrame.origin.y == 0) {
        hold_chooseViewFrame = self.contentOfChoosePickerView.frame;
    }
    if (self.contentOfChoosePickerView.frame.origin.y == hold_chooseViewFrame.origin.y) {
        return;
    }
    [UIView animateWithDuration:.35 animations:^{
        self.contentOfChoosePickerView.frame = hold_chooseViewFrame;
    }];

}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (hold_model) {
        return hold_model.info_list.count;
    }else{
        return 0;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DDMyQuestionCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([DDMyQuestionCollectionViewCell class]) forIndexPath:indexPath];

    DDModelMyQuetionsListElement * element = hold_model.info_list[indexPath.row];
    cell.label_userName.text = element.info_userName;

    NSTimeInterval createTime = [[element.info_createTime substringWithRange:timeRange] integerValue];
    NSDate * myDate = [NSDate dateWithTimeIntervalSince1970:createTime];
    cell.label_advanceDate.text = [DateUtil compareCurrentTime:myDate];


    cell.label_answerNumber.text = [NSString stringWithFormat:@"%@人回答",element.info_answerNumber];

    if (element.info_type.integerValue == DDType_question_questionPublishType_Encryption) {
        cell.label_passward.text = [NSString stringWithFormat:@"密码:%@",element.info_password];
    }

    DDModelMyQuetionsListElement_answerInfo * answerInfo = [element.info_questions firstObject];
    BOOL isHaveImage = NO;
    cell.label_body.text = @"";
    if (answerInfo) {
        for (DDModelMyQuetionsListElement_answerInfo_body * tempBody in answerInfo.info_body) {
            if ([tempBody.info_type isEqualToString:@"TEXT"]) {
                cell.label_body.text = tempBody.info_text;
            }else if ([tempBody.info_type isEqualToString:@"IMAGE"]){
                isHaveImage = YES;
                [cell.imageView_body sd_setImageWithURL:[NSURL URLWithString:tempBody.info_url] placeholderImage:[UIImage imageNamed:@"首页-答疑-客观题-单选习题拍照图-通用"]];
            }
        }
    }
    if (isHaveImage == YES) {
        cell.imageView_body.frame = CGRectMake(8, 79, CGRectGetWidth([[UIScreen mainScreen] bounds]) - 16, 80);
    }else{
        cell.imageView_body.frame = CGRectMake(8, 79, CGRectGetWidth([[UIScreen mainScreen] bounds]) - 16, 0);
    }

    cell.label_course.text = @"";
    if (element.info_questions.count > 1) {
        cell.imageView_course_bg.image = [UIImage imageNamed:@"题目组背景"];
        if (element.info_courseName) {
            cell.label_course.text = [NSString stringWithFormat:@"%@/%@",element.info_courseName,[NSNumber numberWithInteger:element.info_questions.count]];
            
            CGSize size = [self autoLabelString:self.view string:cell.label_course.text];
            cell.imageView_course_bg.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - size.width, cell.imageView_course_bg.frame.origin.y, size.width, cell.imageView_course_bg.frame.size.height);
            cell.label_course.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - size.width, cell.label_course.frame.origin.y, size.width, size.height);
            cell.contentOfReward.frame = CGRectMake(cell.label_userName.frame.origin.x+cell.label_userName.frame.size.width, cell.contentOfReward.frame.origin.y, cell.contentOfReward.frame.size.width, cell.contentOfReward.frame.size.height);
        }else{
            cell.label_course.text = [NSString stringWithFormat:@"/%@",[NSNumber numberWithInteger:element.info_questions.count]];
            
            CGSize size = CGSizeMake(80, 17);
            cell.imageView_course_bg.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - size.width, cell.imageView_course_bg.frame.origin.y, size.width, cell.imageView_course_bg.frame.size.height);
            cell.label_course.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - size.width, cell.label_course.frame.origin.y, size.width, size.height);
            cell.contentOfReward.frame = CGRectMake(cell.label_userName.frame.origin.x+cell.label_userName.frame.size.width, cell.contentOfReward.frame.origin.y, cell.contentOfReward.frame.size.width, cell.contentOfReward.frame.size.height);
        }
        
        
    }else{
        cell.imageView_course_bg.image = [UIImage imageNamed:@"学科背景"];
        if (element.info_courseName) {
            cell.label_course.text = element.info_courseName;
            
            CGSize size = [self autoLabelString:self.view string:cell.label_course.text];
            cell.imageView_course_bg.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - size.width, cell.imageView_course_bg.frame.origin.y, size.width, cell.imageView_course_bg.frame.size.height);
            cell.label_course.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - size.width, cell.label_course.frame.origin.y, size.width, size.height);
            cell.contentOfReward.frame = CGRectMake(cell.label_userName.frame.origin.x+cell.label_userName.frame.size.width, cell.contentOfReward.frame.origin.y, cell.contentOfReward.frame.size.width, cell.contentOfReward.frame.size.height);
            
        }else{
            cell.label_course.text = @"";
            
            CGSize size = CGSizeMake(80, 17);
            cell.imageView_course_bg.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - size.width, cell.imageView_course_bg.frame.origin.y, size.width, cell.imageView_course_bg.frame.size.height);
            cell.label_course.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - size.width, cell.label_course.frame.origin.y, size.width, size.height);
            cell.contentOfReward.frame = CGRectMake(cell.label_userName.frame.origin.x+cell.label_userName.frame.size.width, cell.contentOfReward.frame.origin.y, cell.contentOfReward.frame.size.width, cell.contentOfReward.frame.size.height);
        }
    }
    
//    [cell.contentView addSubview:cell.imageView_course_bg];
//    [cell.contentView addSubview:cell.label_course];
    
    
    
    
    
    
    

    cell.info_publishType = element.info_type.integerValue;

    if (element.info_reward) {
        cell.label_reward.text = element.info_reward;
    }else{
        cell.label_reward.text = @"";
    }

    if (element.info_showAnswer.boolValue == YES) {
        [cell.btn_releaseAnswer setTitle:@"隐藏答案" forState:UIControlStateNormal];
    }else{
        [cell.btn_releaseAnswer setTitle:@"公开答案" forState:UIControlStateNormal];
    }


    cell.delegate = self;
    [cell fixCell];
    return cell;
}


- (CGSize)autoLabelString:(UIView *)view string:(NSString *)s
{
    UIFont *font = [UIFont fontWithName:@"Arial" size:17.0];
    
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    
    NSDictionary * attributes = @{NSFontAttributeName : font,
                                  NSParagraphStyleAttributeName : paragraphStyle};
    
    CGSize contentSize = [s boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, MAXFLOAT)
                                         options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                      attributes:attributes
                                         context:nil].size;
    return contentSize;
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    float addHeight = 0.0;

    DDModelMyQuetionsListElement * element = hold_model.info_list[indexPath.row];

    DDModelMyQuetionsListElement_answerInfo * answerInfo = [element.info_questions firstObject];
    BOOL isHaveImage = NO;
    if (answerInfo) {
        for (DDModelMyQuetionsListElement_answerInfo_body * tempBody in answerInfo.info_body) {
           if ([tempBody.info_type isEqualToString:@"IMAGE"]){
               isHaveImage = YES;
               break;
           }
        }
    }


    if (answerInfo) {
        for (DDModelMyQuetionsListElement_answerInfo_body * tempBody in answerInfo.info_body) {
            if ([tempBody.info_type isEqualToString:@"TEXT"]) {
                CGRect tempRect = [tempBody.info_text boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15],NSParagraphStyleAttributeName : paragraphStyle} context:nil];

                addHeight += (tempRect.size.height - 21);
            }
        }
    }




    CGSize tempSize;
    tempSize.width = self.view.frame.size.width;
    if (isHaveImage == YES) {
        tempSize.height = 245 + addHeight;
    }else{
        tempSize.height = 160 + addHeight;
    }

    return tempSize;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    DDModelMyQuetionsListElement * element = hold_model.info_list[indexPath.row];
    DDMyQuestionDetailViewController * detail = [[DDMyQuestionDetailViewController alloc]init];
    detail.info_element = element;
    [self.navigationController pushViewController:detail animated:YES];
}


#pragma mark - UIPickerViewDataSource & delegate

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return array_questionPublishType.count;
    }else if (component == 1){
        return array_courseType.count;
    }else{
        return array_questionType.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        return array_questionPublishType[row];
    }else if (component == 1){
        ModelQuestionCourseElement * tempElement = array_courseType[row];
        return tempElement.info_title;
    }else{
        return array_questionType[row];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        param_temp_currentChoosedPublishType = array_questionPublishType[row];
        if (row == 0) {
            [array_courseType removeAllObjects];
            [array_courseType addObject:holdCourseElement];
        }else if (row == 1){
            [array_courseType removeAllObjects];
            [array_courseType addObject:holdCourseElement];
            [array_courseType addObjectsFromArray:array_question_encryption];

        }else if (row == 2){
            [array_courseType removeAllObjects];
            [array_courseType addObject:holdCourseElement];
            [array_courseType addObjectsFromArray:array_question_reward];

        }else{
            [array_courseType removeAllObjects];
            [array_courseType addObject:holdCourseElement];
            [array_courseType addObjectsFromArray:array_question_help];
        }
        [pickerView reloadComponent:1];
    }else if (component == 1){
        param_temp_choosedCourse = array_courseType[row];
    }else{
        param_temp_choosedQuestionType = array_questionType[row];
    }
}


#pragma mark - DDMyQuestionCollectionViewCellDelegate

-(void)pressBtn_answerCount:(DDMyQuestionCollectionViewCell *)cell{
    NSInteger row = [[self.myCollectionView indexPathForCell:cell] row];
    DDModelMyQuetionsListElement * element = hold_model.info_list[row];
    NSDictionary * tempDic = [element toDictionary];

    DDTopicDataCellModel *topicModel = [[DDTopicDataCellModel alloc]initWithDictionary:tempDic error:nil];

    DDZdtjVC * zdtjVC = [[DDZdtjVC alloc]init];
    zdtjVC.topicId = topicModel.id;
    zdtjVC.showAnswer = YES;//topicModel.showAnswer;
    zdtjVC.showAnswer = YES;//element.info_showAnswer.boolValue;
    [self.navigationController pushViewController:zdtjVC animated:YES];


//    weakSelf.zdtj = [[DDZdtjVC alloc] initWithNibName:@"DDZdtjVC" bundle:nil];
//    weakSelf.zdtj.topicId = topicModel.id;
//    weakSelf.zdtj.showAnswer = YES;
//    //UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:weakSelf.zdtj];
//
//    //[weakSelf presentViewController:nav animated:YES completion:nil];
//    //[weakSelf.navigationController pushViewController:weakSelf.zdtj animated:YES];
//    //[[NSNotificationCenter defaultCenter] postNotificationName:@"switchVC" object:weakSelf.zdtj];
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"ToDetailPage" object:weakSelf.zdtj];


}
-(void)pressBtn_updatePwd:(DDMyQuestionCollectionViewCell *)cell{
    NSInteger row = [[self.myCollectionView indexPathForCell:cell] row];
    holdUpdatePwd_model = hold_model.info_list[row];
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"修改密码" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert show];
}
-(void)pressBtn_republish:(DDMyQuestionCollectionViewCell *)cell{
    NSInteger row = [[self.myCollectionView indexPathForCell:cell] row];
    DDModelMyQuetionsListElement * element = hold_model.info_list[row];
    holdRepublish_model = element;
    NSInteger choosedNumber_integer = element.info_questions.count;
    if (choosedNumber_integer > 1) {
        UIActionSheet * actionSheet = [[UIActionSheet alloc]initWithTitle:@"请选择分类" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"加密",@"悬赏", nil];
        [actionSheet showInView:self.view];
    }else if (choosedNumber_integer == 1){
        UIActionSheet * actionSheet = [[UIActionSheet alloc]initWithTitle:@"请选择分类" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"加密",@"悬赏",@"大家帮", nil];
        [actionSheet showInView:self.view];
    }
}
-(void)pressBtn_publishAnswer:(DDMyQuestionCollectionViewCell *)cell{
    NSInteger row = [[self.myCollectionView indexPathForCell:cell] row];
    DDModelMyQuetionsListElement * element = hold_model.info_list[row];

    NSDictionary * tempDic = @{@"id":[NSNumber numberWithInteger:element.info_id.integerValue],@"showAnswer" : [NSNumber numberWithBool:!element.info_showAnswer.boolValue]};
    [client mainTopicEdit:tempDic completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
        if (isSuccess == YES) {
//            [self.myCollectionView headerBeginRefreshing];
            [self getData];
        }
    }];
}

-(void)pressBtn_changeCourse:(DDMyQuestionCollectionViewCell *)cell{
    NSInteger row = [[self.myCollectionView indexPathForCell:cell] row];
    DDModelMyQuetionsListElement * element = hold_model.info_list[row];
    holdChangeCourse_model = element;

    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"是否修改科目归类" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ([alertView.title isEqualToString:@"修改密码"] && buttonIndex == 1) {
        UITextField * tempTf = [alertView textFieldAtIndex:0];
        if (holdUpdatePwd_model && tempTf.text.length > 0) {
            NSDictionary * tempDic = @{@"id":[NSNumber numberWithInteger:holdUpdatePwd_model.info_id.integerValue],@"password":tempTf.text};
            [client mainTopicEdit:tempDic completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
                if (isSuccess == YES) {
//                    [self.myCollectionView headerBeginRefreshing];
                    [self getData];
                }
            }];
        }
    }else if ([alertView.title isEqualToString:@"是否修改科目归类"] && buttonIndex == 1){
            DDQuestionChooseCourseViewController * vc = [[DDQuestionChooseCourseViewController alloc]init];
            vc.delegate = self;
            vc.info_publishType = holdChangeCourse_model.info_type.integerValue;
            [self.navigationController pushViewController:vc animated:YES];
//            NSDictionary * tempDic = @{@"id":[NSNumber numberWithInteger:holdChangeCourse_model.info_courseId.integerValue],@"title" : tempTf.text};

//            [client mainCourseEdit:tempDic completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
//                if (isSuccess == YES) {
//                    [self.myCollectionView headerBeginRefreshing];
//                }
//            }];

            

    }
}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

    NSMutableArray * tempArray = [NSMutableArray array];
    for (int i = 0; i < holdRepublish_model.info_questions.count; i++) {
        DDModelMyQuetionsListElement_answerInfo * detail = holdRepublish_model.info_questions[i];
        [tempArray addObject:[NSNumber numberWithInteger:detail.info_id.integerValue]];
    }

    if (buttonIndex == 0) {
        //加密
        DDQuestionBaseInfoViewController * baseInfo = [[DDQuestionBaseInfoViewController alloc]init];
        baseInfo.info_questions = tempArray;
        baseInfo.info_publishType = DDType_question_questionPublishType_Encryption;
        [self.navigationController pushViewController:baseInfo animated:YES];

    }else if (buttonIndex == 1){
        //悬赏
        DDQuestionBaseInfoViewController * baseInfo = [[DDQuestionBaseInfoViewController alloc]init];
        baseInfo.info_questions = tempArray;
        baseInfo.info_publishType = DDType_question_questionPublishType_Reward;
        [self.navigationController pushViewController:baseInfo animated:YES];

    }else if (buttonIndex == 2){
        if (actionSheet.numberOfButtons == 4) {
            //大家帮
            DDQuestionBaseInfoViewController * baseInfo = [[DDQuestionBaseInfoViewController alloc]init];
            baseInfo.info_questions = tempArray;
            baseInfo.info_publishType = DDType_question_questionPublishType_help;
            [self.navigationController pushViewController:baseInfo animated:YES];
        }else{
            holdRepublish_model = nil;
        }

    }else{
        holdRepublish_model = nil;
    }
    
}

-(void)choosedCourse:(ModelQuestionCourseElement *)element{
    NSDictionary * tempDic = @{
                               @"topicId" : holdChangeCourse_model.info_id,
                               @"courseId" : element.info_id,
                               };
    [client mainTopicTagAdd:tempDic completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
//        [self.myCollectionView headerBeginRefreshing];
        [self getData];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
